package createModel.BuilderModel.directorBuildPlayer;

import lombok.Data;

/**
 * 功能描述: 转换类 + 构造者模式的优化, 多个实体,转转统一构造者
 * 2024/06/07
 * <p>
 * ZhangYi
 */
public class ConvertAndBuilderUpgrade {
    // 导入和数据模型类移到单独的文件中
    @Data
    static class User {
        private String userName;
        private String password;
    }

    @Data
    class Department {
        private String departmentName;
        private String departmentCode;
    }

    interface Converter<T> {
        void apply(AbstractBuild build, T source);
    }

    // Director 类
    public class Director {
        AbstractBuild build;

        public void setBuild(AbstractBuild build) {
            this.build = build;
        }

        public <T> Product buildFrom(Converter<T> converter, T source) {
            converter.apply(build, source);
            return build.getProduct();
        }
    }

    // 转换器实现
    class UserConverter implements Converter<User> {
        @Override
        public void apply(AbstractBuild build, User source) {
            build.buildPartA(source.getUserName());
            build.buildPartB(source.getPassword());
        }
    }

    class DepartmentConverter implements Converter<Department> {
        @Override
        public void apply(AbstractBuild build, Department source) {
            build.buildPartA(source.getDepartmentName());
            build.buildPartB(source.getDepartmentCode());
        }
    }

}
